//
// Created by xili on 2024/9/9 16:55.
// GO BIG OR GO HOME.
//
#include "leetcode.h"

class Solution {
public:
    int minMoves(int target, int maxDoubles) {
        int ans = 0;
        while (target != 1) {
            if (!maxDoubles) {
                ans += target - 1;
                target = 1;
            } else if (target % 2 == 1) {
                ans += 2;
                target = (target - 1) / 2;
                maxDoubles--;
            } else {
                ans++;
                target /= 2;
                maxDoubles--;
            }
        }
        return ans;
    }
};